<template>
  <div class="upload-cover" @click="showCoverSelect">
     <div class="cover-desc" >
        <img  class="cover-img" :src="value" ref="cover-img" alt=""/>
     </div>

     <!--选择封面-->
     <el-dialog
      title="选择封面"
      :visible.sync="dialogVisible"
      append-to-body
      width="35%"
      >
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="素材库" name="image">
            <!--ref有两个作用 1.作用到普通HTML上可以获取到DOM 2.作用到组件上可以获取组件实例-->
             <image-list :is-show-add-btn="false" :isShowAction="false" ref="img-list" :isShowCur="true"/>
        </el-tab-pane>
        <el-tab-pane label="上传图片" name="update">
            <!--<label for="file"><el-avatar class="cover-update" shape="square" :size="150" fit="cover" icon="el-icon-circle-plus-outline" :src="updateImg" ></el-avatar></label>-->
              <img class="cover-update" ref="cover-update" src="" />
            <input type="file"   ref="file" @change="onFileChange">
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onClose">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { updataImage, imgDelete } from '@/api/image'
import imageList from '@/components/image-list.vue'

export default {
  name: 'uploadCover',
  components: {
    imageList
  },
  // props: ['show-image'],
  props: ['value'],
  data () {
    return {
      dialogVisible: false,
      activeName: 'image',
      updateImg: '' // 上传时预览图片
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    handleClick (tab, event) {
      // console.log(tab, event)
    },
    showCoverSelect () {
      this.dialogVisible = true
    },
    onFileChange () {
      const file = this.$refs.file
      const blobData = window.URL.createObjectURL(file.files[0])
      // console.log(blobData)
      this.$refs['cover-update'].src = blobData
      // 解决选择相同文件不触发change事件问题
      // this.$refs.file.value = ''
    },
    onClose () {
      // 如果tab是上传图片，并且input-file有选择文件,才执行上传图片操作
      const file = this.$refs.file.files[0]
      if (this.activeName === 'update') {
        if (!file) {
          return this.$message('请选择文件')
        }
        // 执行上传文件操作
        const fd = new FormData()
        fd.append('image', file)
        updataImage(fd).then(res => {
          this.$refs['cover-img'].src = res.data.data.url
          // 关闭弹窗
          this.dialogVisible = false

          // 将图片地址发送给父组件
          // this.$emit('update-cover', res.data.data.url)
          this.$emit('input', res.data.data.url)
        })
      } else if (this.activeName === 'image') {
        // 还有一种组件通信的方式，父组件直接访问子组件中的数据
        const imgList = this.$refs['img-list']
        const cur = imgList.cur
        if (cur === null) {
          return this.$message('请选择封面图片')
        }
        // 关闭弹窗
        this.dialogVisible = false
        // 修改父组件绑定数据
        this.$emit('input', imgList.imagesList[cur].url)
      }
    }
  }
}
</script>

<style scoped lang="less">
.upload-cover{
  .cover-desc{
    width:150px;
    height: 150px;
    border:1px dashed #000;
    margin:10px;
    position: relative;
    .cover-img{
      height: 150px;
      max-width: 100%;
      margin:0 auto;
    }
  }
}
.cover-update{
  margin:0 auto;
  display: block;
  width: 150px;
}
</style>
